package com.moxiao.yfjh.module.blind.common;

import com.moxiao.yfjh.framework.common.util.date.DateUtils;
import com.moxiao.yfjh.module.blind.controller.admin.comment.vo.MemberCommentCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.commonphrases.vo.CommonPhrasesCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.level.vo.BlindMemberLevelCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.memberReport.vo.MemberReportCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.moment.vo.BlindMemberMomentCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.sessionRecord.vo.SessionRecordCreateReqVO;
import com.moxiao.yfjh.module.blind.dal.dataobject.comment.MemberCommentDO;
import com.moxiao.yfjh.module.blind.dal.dataobject.commonphrases.CommonPhrasesDO;
import com.moxiao.yfjh.module.blind.dal.dataobject.level.BlindMemberLevelDO;
import com.moxiao.yfjh.module.blind.dal.dataobject.memberReport.MemberReportDO;
import com.moxiao.yfjh.module.blind.dal.dataobject.moment.BlindMemberMomentDO;
import com.moxiao.yfjh.module.blind.dal.dataobject.sessionRecord.SessionRecordDO;
import com.moxiao.yfjh.module.blind.service.comment.MemberCommentService;
import com.moxiao.yfjh.module.blind.service.commonphrases.CommonPhrasesService;
import com.moxiao.yfjh.module.blind.service.level.BlindMemberLevelService;
import com.moxiao.yfjh.module.blind.service.memberReport.MemberReportService;
import com.moxiao.yfjh.module.blind.service.moment.BlindMemberMomentService;
import com.moxiao.yfjh.module.blind.service.sessionRecord.SessionRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @version 1.0
 * @Author zhk
 * @Date 2024/1/11 9:48
 **/
@Service
public class GenNoCommonMethod {


    private static final String PREFIX_MOMENT = "DT";
    private static final String  PREFIX_COMMENT = "PJ";
    private static final String  PREFIX_LEVEL = "HYDJ";
    private static final String  PREFIX_COMMON_PHRASES = "CYY";

    private static final String  PREFIX_SESSION_RECORD = "HHJL";

    private static final String  PREFIX_MEMBER_ORDER = "HYDD";
    @Resource
    private BlindMemberMomentService blindMemberMomentService;
    @Resource
    private MemberCommentService memberCommentService;
    @Resource
    private BlindMemberLevelService blindMemberLevelService;
    @Resource
    private CommonPhrasesService commonPhrasesService;

    @Resource
    private SessionRecordService sessionRecordService;
    @Resource
    private MemberReportService memberReportService;
    private static final int MAX_RANDOM_NUMBER = 999999; // 最大的6位数

    /**
     * 增加 momentNo(动态编号)字段
     * @param createReqVO
     */
    public void addMementNo(BlindMemberMomentCreateReqVO createReqVO){
        BlindMemberMomentDO blindMemberMomentDO = blindMemberMomentService.getMemberMomentTodayLast(DateUtils.getTodayStart(), DateUtils.getTodayEnd());
        DecimalFormat formatCount = new DecimalFormat("0000");
        int dateTime = Integer.parseInt(DateUtils.dateTime());
        if (Objects.nonNull(blindMemberMomentDO)){
            String[] user_no = blindMemberMomentDO.getNo().split("_");
            String lastPart = user_no[user_no.length - 1];
            if (user_no.length > 1) {
                Integer num = Integer.parseInt(lastPart) + 1;
                String no = formatCount.format(num);
                createReqVO.setNo(PREFIX_MOMENT + "_" + dateTime + "_" + no);
            }
        }else {
            String moment_no = PREFIX_MOMENT + "_" + dateTime + "_" + "0001";
            Integer num = blindMemberMomentService.getBlindMemberMomentNo(moment_no);
            if (num == 0) {
                createReqVO.setNo(moment_no);
            }
        }
    }

    /**
     * 增加 commentNo(评论编号)字段
     * @param createReqVO
     */
    public void addCommentNo(MemberCommentCreateReqVO createReqVO){
        MemberCommentDO memberCommentDO = memberCommentService.getMemberCommentTodayLast(DateUtils.getTodayStart(), DateUtils.getTodayEnd());
        DecimalFormat formatCount = new DecimalFormat("0000");
        int dateTime = Integer.parseInt(DateUtils.dateTime());
        if (Objects.nonNull(memberCommentDO)){
            String[] user_no = memberCommentDO.getNo().split("_");
            String lastPart = user_no[user_no.length - 1];
            if (user_no.length > 1) {
                Integer num = Integer.parseInt(lastPart) + 1;
                String no = formatCount.format(num);
                createReqVO.setNo(PREFIX_COMMENT + "_" + dateTime + "_" + no);
            }
        }else {
            String comment_no = PREFIX_COMMENT + "_" + dateTime + "_" + "0001";
            Integer num = memberCommentService.getBlindMemberCommentNo(comment_no);
            if (num == 0) {
                createReqVO.setNo(comment_no);
            }
        }
    }

    /**
     * 增加 memberLevelNo(等级编号)字段
     * @param createReqVO
     */
    public void addMemberLevelNo(BlindMemberLevelCreateReqVO createReqVO){
        BlindMemberLevelDO memberLevelDO = blindMemberLevelService.getMemberLevelTodayLast(DateUtils.getTodayStart(), DateUtils.getTodayEnd());
        DecimalFormat formatCount = new DecimalFormat("0000");
        int dateTime = Integer.parseInt(DateUtils.dateTime());
        if (Objects.nonNull(memberLevelDO)){
            String[] user_no = memberLevelDO.getNo().split("_");
            String lastPart = user_no[user_no.length - 1];
            if (user_no.length > 1) {
                Integer num = Integer.parseInt(lastPart) + 1;
                String no = formatCount.format(num);
                createReqVO.setNo(PREFIX_LEVEL + "_" + dateTime + "_" + no);
            }
        }else {
            String comment_no = PREFIX_LEVEL + "_" + dateTime + "_" + "0001";
            Integer num = blindMemberLevelService.getMemberLevelNo(comment_no);
            if (num == 0) {
                createReqVO.setNo(comment_no);
            }
        }
    }


    /**
     * 增加 commonPhrasesNo(等级编号)字段
     * @param createReqVO
     */
    public void addCommonPhrasesNo(CommonPhrasesCreateReqVO createReqVO){
        SessionRecordDO sessionRecordTodayLast = sessionRecordService.getSessionRecordTodayLast(DateUtils.getTodayStart(), DateUtils.getTodayEnd());
        DecimalFormat formatCount = new DecimalFormat("0000");
        int dateTime = Integer.parseInt(DateUtils.dateTime());
        if (Objects.nonNull(sessionRecordTodayLast)){
            String[] user_no = sessionRecordTodayLast.getNo().split("_");
            String lastPart = user_no[user_no.length - 1];
            if (user_no.length > 1) {
                Integer num = Integer.parseInt(lastPart) + 1;
                String no = formatCount.format(num);
                createReqVO.setNo(PREFIX_COMMON_PHRASES + "_" + dateTime + "_" + no);
            }
        }else {
            String comment_no = PREFIX_COMMON_PHRASES + "_" + dateTime + "_" + "0001";
            Integer num = sessionRecordService.getSessionRecordNo(comment_no);
            if (num == 0) {
                createReqVO.setNo(comment_no);
            }
        }
    }

    public static String getCode() {
        int min = 100000; // 最小的6位数
        int randomNum = ThreadLocalRandom.current().nextInt(min, MAX_RANDOM_NUMBER + 1);
        String verificationCode = String.format("%06d", randomNum); // 将随机数格式化为6位字符串
//        System.out.println("验证码：" + verificationCode);
        return verificationCode + "";
    }

    /**
     * 增加 commonPhrasesNo(等级编号)字段
     * @param createReqVO
     */
    public void addSessionNo(SessionRecordCreateReqVO createReqVO){
        CommonPhrasesDO commonPhrasesDO = commonPhrasesService.getCommonPhrasesTodayLast(DateUtils.getTodayStart(), DateUtils.getTodayEnd());
        DecimalFormat formatCount = new DecimalFormat("0000");
        int dateTime = Integer.parseInt(DateUtils.dateTime());
        if (Objects.nonNull(commonPhrasesDO)){
            String[] user_no = commonPhrasesDO.getNo().split("_");
            String lastPart = user_no[user_no.length - 1];
            if (user_no.length > 1) {
                Integer num = Integer.parseInt(lastPart) + 1;
                String no = formatCount.format(num);
                createReqVO.setNo(PREFIX_SESSION_RECORD + "_" + dateTime + "_" + no);
            }
        } else {
            String comment_no = PREFIX_SESSION_RECORD + "_" + dateTime + "_" + "0001";
            Integer num = commonPhrasesService.getCommonPhrasesNo(comment_no);
            if (num == 0) {
                createReqVO.setNo(comment_no);
            }
        }
    }

    /**
     * 增加 reportNo(举报编号)字段
     * @param createReqVO
     */
    public void addReportNo(MemberReportCreateReqVO createReqVO){
        MemberReportDO memberReportDO = memberReportService.getMemberReportTodayLast(DateUtils.getTodayStart(), DateUtils.getTodayEnd());
        DecimalFormat formatCount = new DecimalFormat("0000");
        int dateTime = Integer.parseInt(DateUtils.dateTime());
        if (Objects.nonNull(memberReportDO)){
            String[] user_no = memberReportDO.getNo().split("_");
            String lastPart = user_no[user_no.length - 1];
            if (user_no.length > 1) {
                Integer num = Integer.parseInt(lastPart) + 1;
                String no = formatCount.format(num);
                createReqVO.setNo(PREFIX_COMMENT + "_" + dateTime + "_" + no);
            }
        }else {
            String comment_no = PREFIX_COMMENT + "_" + dateTime + "_" + "0001";
            Integer num = memberReportService.getBlindMemberReportNo(comment_no);
            if (num == 0) {
                createReqVO.setNo(comment_no);
            }
        }
    }


    public static void main(String[] args) {
        String userNo = "HY_20240111_0146";
        String[] user_no = userNo.split("_");
        String lastPart = user_no[user_no.length - 1];
        Integer num = Integer.parseInt(lastPart) + 1;
        DecimalFormat formatCount = new DecimalFormat("0000");
        String no = formatCount.format(num);
//        System.out.println("HY" + "_" + Integer.parseInt(DateUtils.dateTime()) + "_" + no);
    }
}
